package com.salim.leetcode.$71;

import java.util.Deque;
import java.util.LinkedList;

public class SimplifyPath {
    public String simplifyPath(String path) {
        if(path.length()==0){
            return "";
        }
        Deque<String> deque = new LinkedList<>();
        String[] array = path.split("/");
        for(int i=0;i<array.length;i++){
            if(array[i].equals(".") ||array[i].equals("")  ){
                continue;
            }else if(array[i].equals("..")){
                deque.poll();
            }else{
                deque.push(array[i]);
            }
        }

        if(deque.size()==0){
            return "/";
        }else{
            StringBuilder result = new StringBuilder();
            while(deque.size() != 0) result.append("/").append(deque.pollLast());
            return result.toString();
        }
    }

    public static void main(String[] args) {
        SimplifyPath s = new SimplifyPath();
        //s.simplifyPath("/home/");
        //s.simplifyPath("/../");
        //s.simplifyPath("/home//foo/");
        //s.simplifyPath("/a/./b/../../c/");
        //s.simplifyPath("/a//b////c/d//././/..");
//        s.simplifyPath("/...");
    }
}
